package yonghe
{
	import flash.events.Event;
	import flash.display.Shape;
	import flash.display.Sprite;
	public class LunarSelect extends Sprite
	{
		public static var stageW:uint=480;
		public static var stageH:uint=800;
		private var date_str:String=null;
		public function LunarSelect(_date_str:String="2017-11-22")
		{
			date_str = _date_str;

			if(stage){
				stageW = stage.stageWidth;
				stageH = stage.stageHeight;
			}else{
				stageW = test.stageW;
				stageH = test.stageH;
			}

			make_date();
			canceled();
		}
		private var year_input:TxtInput;
		private var month_input:TxtInput;
		private var day_input:TxtInput;
		private var txt_size:int = 45;
		private function make_date():void
		{
		//public function TxtInput(txtSize:int,_w:int,_leftTxt:String=null,_rightTxt:String=null,_upTxt:String=null,_color:uint=0x0,_border:Boolean=true)
			var _color:uint = 0x0;
			year_input = new TxtInput(txt_size,txt_size*2.2,null,"年",null,_color,false);
			year_input.addEventListener(Event.SELECT,show_year_list);
			year_input.addEventListener(Event.CHANGE,commit);
			addChild(year_input);

			month_input= new TxtInput(txt_size,txt_size*1.2,null,"月",null,_color,false);
			month_input.addEventListener(Event.SELECT,show_month_list);
			month_input.addEventListener(Event.CHANGE,commit);
			addChild(month_input);
			month_input.x = year_input.width;

			day_input= new TxtInput(txt_size,txt_size*1.2,null,"日",null,_color,false);
			day_input.addEventListener(Event.SELECT,show_day_list);
			day_input.addEventListener(Event.CHANGE,commit);
			addChild(day_input);
			day_input.x = month_input.x + month_input.width;

			//year_input.inputTxt.autoSize = month_input.inputTxt.autoSize = day_input.inputTxt.autoSize = "center";
		}
		private function show_year_list(e:Event):void
		{
			if(year_input.value.length==0)
			{
				var _arr:Array = [2017];
				var _i:int = 2017;
				var _curYear:int = new Date().getFullYear();
				while(_curYear>i)
				{
					++_i;
					_arr.push(_i);
				}
				year_input.show_selectlist(_arr);
				year_input.show_selectlist(null,true);
			}else if(year_input.value.length<=4){
				var old:String = year_input.value;
				if(int(year_input.value)>=210)
				{
					old = year_input.value.substr(0,year_input.value.length-1);
				}

				var arr:Array = new Array();
				var i:int = 0;
				while(i<10)
				{
					arr.push(old+i);
					++i;
				}
				year_input.show_selectlist(arr);
				year_input.show_selectlist(null,true);
			}
			dispatchEvent(e);
		}
		private function show_month_list(e:Event):void
		{
			month_input.show_selectlist(["1","2","3","4","5","6","7","8","9","10","11","12"]);
			month_input.show_selectlist(null,true);
			dispatchEvent(e);
		}
		private function show_day_list(e:Event):void
		{
			day_input.show_selectlist(["1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31"]);
			day_input.show_selectlist(null,true);
			dispatchEvent(e);
		}
		public function hide():void
		{
			canceled();
		}
		private function canceled(e:Event=null):void
		{
			trace("canceled");
			year_input.value="";
			month_input.value="";
			day_input.value="";
			if(date_str){
				var arr:Array = date_str.match(/[\d]+/g);
				if(arr.length>=3){
					year_input.value = arr[0];
					month_input.value = arr[1];
					day_input.value = arr[2];
				}
			}
			if(year_input.value=="0000")year_input.value = "";
			if(month_input.value=="00")month_input.value = "";
			if(day_input.value=="00")day_input.value = "";
			if(e)visible = false;
		}
		public function get date_value():Array
		{
			return [get_value(year_input.value),get_value(month_input.value),get_value(day_input.value)];

		}
		private function get_value(s:String):int
		{
			if(s){
				return int(s.replace(/[\r\n\s\\\/]/g,""));
			}
			return 0;
		}
		private function commit(e:Event=null):void
		{
			dispatchEvent(new Event(Event.COMPLETE));
		}
	}
}

